#include"include.h"

int isInt(char c)
{
   return c >= '0' && c <= '9';
}

char *mysubstr(char *s, int b, int len)
{
   char *ret = malloc(len + 1);
   int i;
   for(i=0;i<len;i++)
      ret[i] = s[b+i];
   ret[i] = '\0';
   return ret;
}

mystr *mystrcats(mystr *a, char *b)
{
   long blen = strlen(b);
   if(a->maxlen == 0)
      a->maxlen = a->len;
   while(a->len + blen > a->maxlen)
   {
      a->maxlen += 100;
      a->s = realloc(a->s, a->maxlen + 1);
   }
   int i;
   for(i=a->len;i<(blen + a->len);i++)
   {
      a->s[i] = b[i - a->len];
   }
   a->len += blen;
   return a;
}

mystr *mystrcat(mystr *a, mystr *b)
{
   while(a->len + b->len > a->maxlen)
   {
      a->maxlen += 100;
      a->s = realloc(a->s, a->maxlen + 1);
   }

   strcat(a->s, b->s);
   a->len += b->len;
   return a;
}

int validURLChar(char in)
{
   return (in >= '0' && in <= '9') || (in >= 'a' && in <= 'z') ||
      (in >= 'A' && in <= 'Z') || in == '.' || in == '-' || in == '_' ||
      in == '~';
}

// Given an int between 0 and 15, gives an ascii char representing
// the hex value
char htoa(int data)
{
   char asciihex;
   if(data <= 9)
      asciihex = data + '0';
   else
      asciihex = data + 55;
   return asciihex;
}

char *urlencode(unsigned char *src, int len)
{
   int i;
   unsigned char *source = src;
   char *dest = malloc(len * 3 + 1);
   char *start = dest;

   for(i = 0; i < len; i++)
   {
      if(validURLChar(*source))
      { // Leave valid characters as-is
         *dest = *source;
         dest++;
         source++;
      }
      else
      { // Escape the char
         char fh = htoa((*source) >> 4);
         char sh = htoa((*source) & 0x0f);
         *dest++ = '%';
         *dest++ = fh;
         *dest++ = sh;
         source++;
      }
   }
   // NULL terminate the new string
   *dest = '\0';

   return start;
}

char *randUrlStr(unsigned int length)
{
   char *vals = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_~";
   //Length is currently 10 + 26 + 26 + 4 = 66. Indexex are 0-65
   char *ret = malloc(length+1);
   int i;
   for(i=0;i<length;i++)
   {
      ret[i] = vals[(rand() % 66)];
   }
   //Null terminate I guess
   ret[length] = '\0';
   return ret;
}

char *numUrlStr(unsigned int num, unsigned int length)
{
   char *vals = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-_~";
   //Length is currently 10 + 26 + 26 + 4 = 66. Indexex are 0-65
   char *ret = malloc(length+1);
   int i;
   for(i=length-1;i>=0;i--)
   {
      ret[i] = '0';
      if(num != 0) 
      {
	 ret[i] = vals[num % 66];
	 num = num / 66;
      }
   }

   //Null terminate I guess
   ret[length] = '\0';
   return ret;
}

int mystrStartsWith(char *str, char *fragment)
{
   int i;
   if(strlen(str) < strlen(fragment))
      return 0;
   for(i=0;i<strlen(fragment);i++)
      if(str[i] != fragment[i]) return 0;
   return 1;
}

char *strtolower(char *str)
{
   int i;
   for(i=0;i<strlen(str);i++)
      str[i] = tolower(str[i]);
   return str;
}

int rawstrcmp(unsigned char *l, unsigned char *r, int len)
{
   int i;
   for(i=0;i<len;i++)
      if(l[i] != r[i]) return 0;
   return 1;
}

void mystrfree(mystr *s)
{
   free (s->s);
   free(s);
}
